#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <fstream>
#include <set>

using namespace std;

class Solution {
    public:
        int lengthOfLongestSubstring(string s) {
            int hash[256] = {0};
            int ans = 0, start = 0;

            for (int i = 0; i < s.size(); i++) {
               if (hash[s[i] - 'a'] == 1) {
                   cout << i << " - " << start << endl;
                   ans = max(ans, i - start);    
                   while (s[start] != s[i]){
                        hash[s[start] - 'a'] = 0;
                        start++;
                   }
                   start++;
//                 cout << " Set new start: " << start << endl;
               }
               hash[s[i] - 'a'] = 1;
            }
            int last = s.size()-start;
            ans = max(ans, last);
            //cout << ans << endl;
            return ans;
        }
};

//add new code just write
    int lengthOfLongestSubstring(string s) {
        set<char> mySet;
        int start = 0;
        int longest = 0;
        for (int i = 0; i < s.size(); i++) {
            if (mySet.count(s[i]) == 0) {
                mySet.insert(s[i]); 
                continue;
            }   
            longest = max(longest ,i - start); 
            while(s[start] != s[i]) {
                mySet.erase(s[start]);
                start++;
            }   
            start++;
        }   
        longest = max(longest, (int)s.size() - start); 
        return longest;
    } 

//test in 1337 online judge and pass all the test
int main(int argc, char **argv)
{
    Solution mySolution;
    int  result;

    cout << "-----------------Test 1--------------------" << endl;
    result = mySolution.lengthOfLongestSubstring("abcabcbb");
    cout << result << endl;

    cout << "-----------------Test 2--------------------" << endl;
    result = mySolution.lengthOfLongestSubstring("abcddcbae");
    cout << result << endl;

    cout << "-----------------Test 3--------------------" << endl;
    result = mySolution.lengthOfLongestSubstring("abcdcefcg");
    cout << result << endl;

    cout << "-----------------Test 4--------------------" << endl;
    result = mySolution.lengthOfLongestSubstring("bsvvwqpgfpcghedmpeyujbmdzcjchokyxlpvmaen");
    cout << result << endl;

}
